import time
from django.shortcuts import render,HttpResponse,redirect
from django.core import serializers
from utils.myopenid import OpenidUtils
from django.http import JsonResponse
from wechat_mini import models
from django.db.models import Q
import hashlib
import datetime
import json
# Create your views here.


#微信登录
def wxlogin(request):
    print(request.POST)
    back_dic={
            "code":'',
            "token":''
    }
    username=request.POST.get('xuehao')
    password=request.POST.get('password')
    code=request.POST.get('code')
    json_response = OpenidUtils(code).get_openid()
    if json_response:
        # val = json_response['session_key'] + "&" + json_response['openid']
        val=username
        user_obj = models.StudentInfo.objects.filter(snum=username).first()
        if user_obj:
            if username==user_obj.snum and password==user_obj.spwd:
                if not user_obj.sopenid:
                    models.StudentInfo.objects.update(sopenid=json_response['openid'])
                back_dic["token"] = val
                back_dic["code"]=200
                return JsonResponse(back_dic)
            else:
                back_dic["code"]=201
                return JsonResponse(back_dic)
#修改密码
def changepwd(request):
    print(request.POST)
    back_dic={
            "code":'',
            "msg":''
    }
    xuehao=request.POST.get('xuehao')
    password=request.POST.get('password')
    user_obj=models.StudentInfo.objects.filter(snum=xuehao).first()
    if user_obj:
        if user_obj.snum==xuehao:
            models.StudentInfo.objects.filter(snum=xuehao).update(spwd=password)
            back_dic["code"]=200
            back_dic["msg"]='修改成功,请返回登录！'
            return JsonResponse(back_dic)
        back_dic["code"]=201
        back_dic["msg"]='发生未知错误!'
        return JsonResponse(back_dic)
    back_dic["code"] = 202
    back_dic["msg"] = '用户不存在！！'
    return JsonResponse(back_dic)
#获取用户信息
def getuserdetail(request):
    # print(request.body)
    json_str=json.loads(request.body)
    # print(json_str['stuNum'])
    user_obj=models.StudentInfo.objects.filter(snum=json_str['stuNum'])
    res = serializers.serialize('json', user_obj)
    # user_obj=models.StudentInfo.objects.filter()
    return HttpResponse(res)
#更新个性签名
def updatesign(request):
    print(request.POST)
    back_dic={
        "code":'',
        "msg":''
    }
    stunum=request.POST.get('stuNum')
    signature=request.POST.get('signature')
    print(stunum,signature)
    user_obj=models.StudentInfo.objects.filter(snum=stunum).first()
    if user_obj.snum==stunum:
        models.StudentInfo.objects.filter(snum=stunum).update(ssignature=signature)
    back_dic["code"]=200
    back_dic["msg"]='个性签名修改成功！！'
    return JsonResponse(back_dic)
#获取考勤记录
def getkaoqin(request):
    # print(request.body)
    json_str = json.loads(request.body)
    kqinfo_obj=models.KaoQin.objects.filter(stunum=json_str['stuNum'])
    # print(json_str['stuNum'])
    res=serializers.serialize('json',kqinfo_obj)
    return HttpResponse(res)
#获取请假申请记录
def stuleave(request):
    json_str = json.loads(request.body)
    stuleave_obj=models.StudentLeave.objects.filter(stunum=json_str['stuNum'])
    # print(json_str['stuNum'])
    res=serializers.serialize('json',stuleave_obj)
    return HttpResponse(res)
#获取学生课程
def stucourse(request):
    json_str = json.loads(request.body)
    stucourse_obj=models.StudentCourse.objects.filter(snum=json_str['stuNum'])
    res = serializers.serialize('json', stucourse_obj)
    return HttpResponse(res)
#增加请假记录
def askLeave(request):
    back_dic = {
        "code":'',
        "msg": ''
    }
    json_str = json.loads(request.body)
    print(json_str)
    user_obj=models.StudentLeave.objects.filter(stunum=json_str['stuNum']).first()
    if user_obj:
        models.StudentLeave.objects.create(
            stunum=user_obj.stunum,
            stuname=user_obj.stuname,
            teaname=user_obj.teaname,
            course=json_str['course'],
            leavetime=json_str['leaveTime'],
            stureason=json_str['reason'],
            applyTime=datetime.datetime.now(),
            status='待审核'
        )
    back_dic["code"]=1000
    back_dic["msg"]="插入成功"
    return JsonResponse(back_dic)
 #签到
def qiandao(request):
    back_dic = {"msg": ''}
    statue_code={"ok":"ok","no":"no","out":"out","exist":"exist","end":"end"}
    seat=request.GET.get('seat')
    line=request.GET.get('line')
    column=request.GET.get('column')
    token=request.GET.get('token')
    seatcoodinates=line+"-"+column
    user_obj=models.KaoQin.objects.filter(stunum=token).first()
    if user_obj:
        models.KaoQin.objects.create(stuname=user_obj.stuname,stunum=token,seatnum=seat,seatcoordinates=seatcoodinates,attendanceresult=1,attendancetime=datetime.datetime.now())
        back_dic["msg"]=statue_code.get("ok")+"-"+seat+"-"+user_obj.stuname
        # back_dic["msg"]="end-"+seat+"-"+user_obj.stuname
    return JsonResponse(back_dic)
#补签
def buqian(request):
    back_dic = {
        "code":200,
        "msg": ''
    }
    print(request.POST)
    print(request.body)
    return JsonResponse(back_dic)
